const mysql = require('mysql2');
require('dotenv').config();

// 创建连接池
const pool = mysql.createPool({
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || 'root',
    database: process.env.DB_NAME || 'pet_adoption',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
}).promise(); // 使用promise包装，支持async/await

// 测试数据库连接
async function testConnection() {
    try {
        const connection = await pool.getConnection();
        console.log('数据库连接成功');
        connection.release();
        return true;
    } catch (err) {
        console.error('数据库连接失败:', err);
        return false;
    }
}

module.exports = { pool, testConnection }; 